package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author ChenXing
 * @Description    在 common 模块下的 @service : 公共业务层
 * @Create 2024/8/16 18:05
 */

@Service
public class RabbitService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // String exchange：消息交换机的名称，消息将被发送到这个交换机。
    // String routingKey：路由键，用于将消息路由到正确的队列。
    // Object message：要发送的消息对象，它将被转换为序列化的形式（通常是JSON）

    public boolean sendMessage(String exchange, String routingKey, Object message) {

        // 为了实现可靠性，发送消息之前先将消息内容存入缓存
        String id = UUID.randomUUID().toString();
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        guiguCorrelationData.setRetryCount(6);//给你六次重试机会
        guiguCorrelationData.setMessage(message);
        guiguCorrelationData.setDelay(false);
        guiguCorrelationData.setRoutingKey(routingKey);
        guiguCorrelationData.setExchange(exchange);
        guiguCorrelationData.setId(id);

        redisTemplate.opsForValue().set(id, JSON.toJSONString(guiguCorrelationData));

        rabbitTemplate.convertAndSend(exchange, routingKey, message,guiguCorrelationData);

        return true;

    }


    public boolean sendDeadMessage(String exchange_dead, String routing_dead_1, String hello_dead) {

        // 为了实现可靠性，发送消息之前先将消息内容存入缓存
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        String id = java.util.UUID.randomUUID().toString();
        System.out.println("首次发送消息，生成消息id："+id);
        guiguCorrelationData.setId(id);
        guiguCorrelationData.setRetryCount(3);
        guiguCorrelationData.setMessage(hello_dead);
        guiguCorrelationData.setDelay(true);// 测试死信，将重试关闭
        guiguCorrelationData.setExchange(exchange_dead);
        guiguCorrelationData.setRoutingKey(routing_dead_1);
        redisTemplate.opsForValue().set(id,  JSON.toJSONString(guiguCorrelationData));// 为了投递失败存储原始消息


        rabbitTemplate.convertAndSend(exchange_dead, routing_dead_1, hello_dead, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("15000");// 设置消息过期时间,队列过期时间在这里设置的是15秒
                return message;
            }
        }, guiguCorrelationData);// 为了发送失败存储原始消息

        return true;

    }

    public boolean sendDelayMessage(String exchange_delay, String routing_delay, String hello_delay) {

        // 为了实现可靠性，发送消息之前先将消息内容存入缓存
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        String id = java.util.UUID.randomUUID().toString();
        System.out.println("首次发送消息，生成消息id："+id);
        guiguCorrelationData.setId(id);
        guiguCorrelationData.setRetryCount(3);
        guiguCorrelationData.setMessage(hello_delay);
        guiguCorrelationData.setDelay(true);// 测试死信，将重试关闭
        guiguCorrelationData.setExchange(exchange_delay);
        guiguCorrelationData.setRoutingKey(routing_delay);
        redisTemplate.opsForValue().set(id,  JSON.toJSONString(guiguCorrelationData));// 为了投递失败存储原始消息


        rabbitTemplate.convertAndSend(exchange_delay, routing_delay, hello_delay, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(60000*3);
                return message;
            }
        }, guiguCorrelationData);
        return true;
    }











}
